// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  try {
    let star = await db.collection('star').where({
      user_id: wxContext.OPENID,
      word_id: event.word_id,
    }).get();
    if (star.data[0] == null) {
      star = false //未收藏
    } else {
      star = true //收藏
    }
    let res = await db.collection('word').where({
      word_id: event.word_id
    }).get();
    return {
      word_id: res.data[0].word_id,
      word: res.data[0].word,
      pronunciation: res.data[0].pronunciation,
      ciXing: res.data[0].ciXing,
      meaning: res.data[0].meaning,
      level: res.data[0].level,
      star: star,
    }
  } catch (e) {
    console.error(e);
  }
}